package com.liuqi.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuqi.common.base.service.AbstractBaseEntityService;
import com.liuqi.common.base.service.CacheService;
import com.liuqi.iot.bean.dto.DeviceDTO;
import com.liuqi.iot.bean.dto.DeviceModelFieldDTO;
import com.liuqi.iot.bean.dto.DeviceModelPublishedDTO;
import com.liuqi.iot.bean.query.DeviceModelPublishedQuery;
import com.liuqi.iot.domain.entity.DeviceModelPublishedEntity;
import com.liuqi.iot.domain.mapper.DeviceModelPublishedMapper;
import com.liuqi.iot.service.DeviceModelPublishedEntityService;
import com.liuqi.iot.service.DeviceEntityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备模型发布结果表服务实现 
 * @author Coder Generator 2025-03-22 16:54:14 
 **/
@Service
public class DeviceModelPublishedEntityServiceImpl extends AbstractBaseEntityService<DeviceModelPublishedEntity, DeviceModelPublishedDTO, DeviceModelPublishedMapper, DeviceModelPublishedQuery> implements DeviceModelPublishedEntityService {
    @Autowired
    @Lazy
    private DeviceEntityService deviceService;

    @Autowired
    private CacheService cacheService;

    @Override
    protected QueryWrapper<DeviceModelPublishedEntity> queryToWrapper(DeviceModelPublishedQuery query) {
        return this.createQueryWrapper()
                .eq(StringUtils.isNotBlank(query.getId()), "id", query.getId())
                .in(null != query.getIds(), "id", query.getIds())
                .orderByDesc("create_time");
    }

    /**
     * 根据设备编号查询模型信息
     */
    @Override
    public DeviceModelPublishedDTO findByDevice(String deviceCode) {
        // 先查询设备信息
        DeviceDTO device = deviceService.findByCode(deviceCode)
                .orElse(null);
        if (null == device) {
            return null;
        }

        String modelId = device.getModelId();
        return cacheService.getOrCache("iot.deviceModel." + modelId, DeviceModelPublishedDTO.class, () -> this.findById(modelId).orElse(null));
    }

    /**
     * 物理删除
     *
     * @param id 记录id
     */
    @Override
    public void deletePhysical(String id) {
        super.deletePhysical(id);
        cacheService.remove("iot.deviceModel." + id);
    }

    /**
     * 根据设备编号获取设备字段列表
     *
     * @param deviceCode 设备编号
     * @return 设备字段列表
     */
    @Override
    public Map<String, DeviceModelFieldDTO> getDeviceFields(String deviceCode) {
        DeviceModelPublishedDTO dto = this.findByDevice(deviceCode);
        if (null == dto) {
            return null;
        }

        List<DeviceModelFieldDTO> fields = dto.getFields();
        Map<String, DeviceModelFieldDTO> map = new HashMap<>(16);
        fields.forEach(field -> map.put(field.getCode(), field));
        return map;
    }
}